package com.chase.passwordmaster.search

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.chase.passwordmaster.components.blurringview.BlurringView
import com.chase.passwordmaster.entity.DecryptedAccount

class SearchRVAdapter(var accountList: List<DecryptedAccount>, var rInfo: RInfo) :
    RecyclerView.Adapter<SearchRVAdapter.VH>() {

    class RInfo(var rvLayout: Int,
                var tvWebsiteId: Int,
                var tvUsernameId: Int,
                var tvName: Int,
                var lName: Int,
                var tvTel: Int,
                var lTel: Int,
                var tvQQ: Int,
                var lQQ: Int,
                var tvWeChat: Int,
                var lWeChat: Int,
                var tvEmail: Int,
                var lEmail: Int,
                var blurLayout: Int,
                var blur: Int,
                var delete: Int,
                var edit: Int)

    inner class VH(itemView: View, rInfo: RInfo) : RecyclerView.ViewHolder(itemView) {
        var tvWebsite: TextView = itemView.findViewById(rInfo.tvWebsiteId)
        var tvUsername: TextView = itemView.findViewById(rInfo.tvUsernameId)
        var tvName: TextView = itemView.findViewById(rInfo.tvName)
        var lName: ConstraintLayout = itemView.findViewById(rInfo.lName)
        var tvTel: TextView = itemView.findViewById(rInfo.tvTel)
        var lTel: ConstraintLayout = itemView.findViewById(rInfo.lTel)
        var tvQQ: TextView = itemView.findViewById(rInfo.tvQQ)
        var lQQ: ConstraintLayout = itemView.findViewById(rInfo.lQQ)
        var tvWeChat: TextView = itemView.findViewById(rInfo.tvWeChat)
        var lWeChat: ConstraintLayout = itemView.findViewById(rInfo.lWeChat)
        var tvEmail: TextView = itemView.findViewById(rInfo.tvEmail)
        var lEmail: ConstraintLayout = itemView.findViewById(rInfo.lEmail)
        var cpBlurLayout: FrameLayout = itemView.findViewById(rInfo.blurLayout)
        var cpBlur: BlurringView = itemView.findViewById(rInfo.blur)
        var cpDelete: View = itemView.findViewById(rInfo.delete)
        var cpEdit: View = itemView.findViewById(rInfo.edit)
    }

    var itemClickListener: ((holder: SearchRVAdapter.VH, position: Int) -> Any)? = null
    var itemLongClickListener: ((holder: SearchRVAdapter.VH, position: Int) -> Any)? = null

    private lateinit var context: Context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        context = parent.context
        val view = LayoutInflater.from(context).inflate(rInfo.rvLayout, parent, false)
        return VH(view, rInfo)
    }

    override fun getItemCount() = accountList.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        val account = accountList[position]

        // Fill content
        holder.tvWebsite.text = account.website
        holder.tvUsername.text = account.username
        account.apply {
            name?.let {
                holder.lName.visibility = View.VISIBLE
                holder.tvName.text = it
            } ?: let { holder.lName.visibility = View.GONE }

            tel?.let {
                holder.lTel.visibility = View.VISIBLE
                holder.tvTel.text = it
            } ?: let { holder.lTel.visibility = View.GONE }

            qq?.let {
                holder.lQQ.visibility = View.VISIBLE
                holder.tvQQ.text = it
            } ?: let { holder.lQQ.visibility = View.GONE }

            weChat?.let {
                holder.lWeChat.visibility = View.VISIBLE
                holder.tvWeChat.text = it
            } ?: let {
                holder.lWeChat.visibility = View.GONE
            }

            email?.let {
                holder.lEmail.visibility = View.VISIBLE
                holder.tvEmail.text = it
            } ?: let { holder.lEmail.visibility = View.GONE }
        }

        holder.cpBlurLayout.visibility = View.GONE
        holder.cpBlurLayout.alpha = 0f

        // Set listener
        itemClickListener?.run { this(holder, position) }
        itemLongClickListener?.run { this(holder, position) }
    }
}
